import { createRouter, createWebHistory, type RouteRecordRaw } from 'vue-router';
import Login from '../views/Login.vue';
import Register from '../views/Register.vue';
import Dashboard from '../views/teacher/Dashboard.vue';
import Dashboard2 from '../views/director/Dashboard2.vue'; // 修正路径
import ExamRegistration from '../views/teacher/ExamRegistration.vue';
import ExamRecords from '../views/teacher/ExamRecords.vue';
import ExamBatchDetail from '../views/teacher/ExamBatchDetail.vue';
import ExamSignup from '../views/teacher/ExamSignUp.vue';
import Messages from '../views/teacher/MessagesView.vue';
import ApprovalView from '../views/director/ApprovalView.vue';
import ApprovalPending from '../views/director/ApprovalPending.vue';
import ApprovalSuccess from '../views/director/ApprovalSuccess.vue';
import ApprovalRejected from '../views/director/ApprovalRejected.vue'; // 使用正确的命名
import ExamList from '../views/director/ExamList.vue';

const routes: Array<RouteRecordRaw> = [
    {
        path: '/',
        redirect: '/login'
    },
    {
        path: '/login',
        name: 'Login',
        component: Login,
        meta: {
            title: '登录 - 研究生监考报名系统',
            requiresAuth: false
        }
    },
    {
        path: '/register',
        name: 'Register',
        component: Register,
        meta: {
            title: '注册 - 研究生监考报名系统',
            requiresAuth: false
        }
    },

    {
        path: '/dashboard',
        name: 'Dashboard',
        component: Dashboard,
        meta: {
            title: '控制台 - 研究生监考报名系统',
            requiresAuth: true
        },
        children: [
            {
                path: 'exam-registration',
                name: 'ExamRegistration',
                component: ExamRegistration,
                meta: { title: '监考报名' }
            },
            {
                path: 'exam-records',
                name: 'ExamRecords',
                component: ExamRecords,
                meta: { title: '监考记录' }
            },
            {
                path: 'messages',
                name: 'Messages',
                component: Messages,
                meta: { title: '消息中心' }
            },
            {
                path: 'exam-batch/:id',
                name: 'ExamBatchDetail',
                component: ExamBatchDetail,
                meta: {
                    title: '监考批次详情',
                    requiresAuth: true
                }
            },
            {
                path: 'exam-signup/:id',
                name: 'ExamSignup',
                component: ExamSignup,
                meta: {
                    title: '监考报名',
                    requiresAuth: true
                }
            }
        ]
    },

    // 研工办主任专属路由
    {
        path: '/dashboard2',
        component: Dashboard2,
        meta: {
            requiresAuth: true,
            allowedRoles: ['director']
        },
        children: [
            {
                path: 'approval/pending',
                name: 'ApprovalPending',
                component: ApprovalPending,
                meta: { title: '报名等待审批' }
            },
            {
                path: 'approval/success',
                name: 'ApprovalSuccess',
                component: ApprovalSuccess,
                meta: { title: '报名成功列表' }
            },
            {
                path: 'approval/failed',
                name: 'ApprovalRejected',
                component: ApprovalRejected,
                meta: { title: '报名失败列表' }
            },
            {
                path: 'approved-list',
                name: 'ApprovedList',
                component: () => import('../views/director/ApprovedList.vue'),
                meta: { title: '同意报名规则' }
            },
            {
                path: 'rejected-list',
                name: 'RejectedList',
                component: () => import('../views/director/RejectedList.vue'),
                meta: { title: '驳回规则' }
            },
            {
                path: 'exam-list',
                name: 'DirectorExamList',
                component: ExamList,
                meta: { title: '监考名单' }
            },
            {
                path: 'messages',
                name: 'DirectorMessages',
                component: () => import('../views/director/Messages.vue'),
                meta: { title: '消息中心' }
            },
            // 默认重定向
            {
                path: '',
                redirect: '/dashboard2/approval/pending'
            }
        ]
    }
];

const router = createRouter({
    history: createWebHistory(import.meta.env.BASE_URL),
    routes
});

router.beforeEach((to, from, next) => {
    const title = to.meta.title as string | undefined;
    document.title = title || '研究生监考报名系统';

    // 检查需要身份验证的路由
    if (to.meta.requiresAuth) {
        const userInfo = localStorage.getItem('userInfo');

        if (!userInfo) {
            next('/login');
            return;
        }

        // 检查角色权限
        const { role } = JSON.parse(userInfo);
        const allowedRoles = to.meta.allowedRoles || ['staff', 'director', 'officer'];

        if (!allowedRoles.includes(role)) {
            alert('您没有访问此页面的权限');
            next('/login');
            return;
        }
    }

    next();
});

export default router;